package com.mlnx.web.data.controller;


import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.form.PageForm;
import com.mlnx.external.push.entity.TPush;
import com.mlnx.external.push.service.TPushDeviceService;
import com.mlnx.external.push.service.TPushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


@Api(value = "推送数据接口",tags={"推送数据接口"})
@RestController
@RequestMapping("push")
public class PushController extends BaseController {
    @Autowired
    private TPushService tPushService;
    @Autowired
    private TPushDeviceService tPushDeviceService;




    @ApiOperation("分页获取所有推送数据")
    @GetMapping(value = "")
    public ResponseData listPage(@Valid PageForm pageForm,HttpServletRequest httpServletRequest) {
        Integer userId = Integer.parseInt((String) httpServletRequest.getAttribute("username"));

        return result(tPushService.findPage(pageForm,userId));
    }



    @ApiOperation("新增推送")
    @PostMapping (value = "")
    public Response addPush(@Valid TPush tPush,HttpServletRequest httpServletRequest) {
        Integer userId = Integer.parseInt((String) httpServletRequest.getAttribute("username"));
        tPush.setUserId(userId);
        tPushService.addPush(tPush);
        return result();
    }

    @ApiOperation("更新推送数据")
    @RequestMapping(value = "{id}", method = {RequestMethod.PUT})
    public Response updatePush(@Valid TPush tPush, @PathVariable("id") Integer id) {
        tPush.setId(id);
        tPushService.updateById(tPush);
        return result();

    }

    @ApiOperation("删除推送数据")
    @RequestMapping(value = "", method = {RequestMethod.DELETE})
    public Response deletePush( @RequestParam(value = "ids", required = true) List<Integer> ids) {
        tPushService.removeByIds(ids);
        return result();

    }
}
